/*
 * Copyright (C) 2004-2009  Autodesk, Inc.
* 
* This library is free software; you can redistribute it and/or
* modify it under the terms of version 2.1 of the GNU Lesser
* General Public License as published by the Free Software Foundation.
* 
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef MAPINFOCLASSDEFINITION_H_
#define MAPINFOCLASSDEFINITION_H_     1

#ifdef _WIN32
#pragma once
#endif

class MapInfoConnection;
class MapInfoFeatureSchema;
class MapInfoLpPropertyDefinition;
class MapInfoLpPropertyDefinitionCollection;

class MapInfoClassDefinition :
    public FdoDisposable
{
    friend class MapInfoFeatureSchema;

public:
    MapInfoClassDefinition(void);
    MapInfoClassDefinition(MapInfoConnection* conn, IMapInfoFile* layer, FdoClassDefinition* fdoClass);
    ~MapInfoClassDefinition(void);
    MapInfoFeatureSchema* GetParent();
    void SetParent(MapInfoFeatureSchema* schema);

    static MapInfoClassDefinition* CreateMapInfoClassDefinition(MapInfoConnection* conn, FdoClassDefinition* fdoClass);

    // Get logical class:
    FdoClassDefinition* GetFdoClass(void);

    bool HasData();
    void DeleteMapInfoFile();
    void ModifyMapInfoFile(FdoClassDefinition* fdoClass);

    // To please FdoNamedCollection:
    FdoString* GetName();
    virtual FdoBoolean CanSetName() { return true; }

    int GetPropertyIndex(FdoString * strPropertyName);
    GByte * GenerateIndexKey(int index, FdoDataValue * val);
    int GetKeyLength(int index);

    IMapInfoFile* GetMapInfoFile();
    bool GetBounds(double &dXMin, double &dYMin, double &dXMax, double &dYMax);
    bool GetExtents(double &dXMin, double &dYMin, double &dXMax, double &dYMax);
    int GetFeatureCount();
    FdoDataPropertyDefinitionCollection* GetIdentityProperty();
    FdoStringP GetIdentityPropertyName();
    FdoStringP GetGeometryPropertyName();

protected:
    static void CreateDataProperty(IMapInfoFile* layer, FdoDataPropertyDefinition* propDef);
    static void CreateGeometricProperty(IMapInfoFile* layer, FdoGeometricPropertyDefinition* propDef);
    static IMapInfoFile* CreateMapInfoFile(MapInfoConnection* conn, FdoClassDefinition* fdoClass, int nIndex = -1);
    static void ValidateProperty(FdoClassDefinition* fdoClass);
    static void AddMissedProperty(FdoClassDefinition* fdoClass);
    static void RemoveDeletedProperty(FdoClassDefinition* oldFdoClass, FdoClassDefinition* newFdoClass);

private:
    FdoPtr<MapInfoConnection> m_connection;
    MapInfoFeatureSchema* m_parent; // NOTE: weak reference
    IMapInfoFile* m_mapinfoLayer;   // NOTE: weak reference
    FdoPtr<FdoClassDefinition> m_fdoClassDefinition;
};

class MapInfoClassDefinitionCollection : public FdoNamedCollection<MapInfoClassDefinition, FdoException>
{
public:
    MapInfoClassDefinitionCollection(void) {}
protected:
    virtual ~MapInfoClassDefinitionCollection(void) {}

    /// <summary>Dispose this object instance.</summary>
    /// <returns>Returns nothing</returns>
    virtual void Dispose() 
    { 
        delete this; 
    }
};


#endif // MAPINFOCLASSDEFINITION_H_

